package org.jackysoft.report;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.jackysoft.entity.ArmItem;
import org.jackysoft.entity.DataItem;
import org.jackysoft.entity.MonthPlanningItem;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Component
public final class ReporterUtil {

	

	/**
	 * 统计当月动工数
	 * 
	 * @param source
	 *            源
	 * @param target
	 *            目标
	 * @param dts
	 *            数据类型
	 * */
	public void statAtom(ArmItem source, ArmItem target, DataItemType[] dts) {
		// 如果没找到就返回,不给予统计...
		if (target == null)
			return;
		for (DataItemType dt : dts) {
			DataItem sd = source.getDataItems().get(dt.toString());
			if (sd == null)
				continue;
			DataItem td = target.getDataItems().get(dt.toString());
			if (td == null) {
				td = new DataItem();
				target.getDataItems().put(dt.toString(), td);
			}
			td.setAtomValue(sd.getAtomValue());

		}
	}

	/**
	 * 统计当月累计数
	 * 
	 * @param source
	 *            源
	 * @param target
	 *            目标
	 * @param dts
	 *            数据类型
	 * */
	public void statAccumulation(ArmItem source, ArmItem target,
			DataItemType[] dts) {
		if (target == null)
			return;
		for (DataItemType dt : dts) {
			DataItem sd = source.getDataItems().get(dt.toString());
			if (sd == null)
				continue;
			DataItem td = target.getDataItems().get(dt.toString());
			if (td == null) {
				td = new DataItem();
				target.getDataItems().put(dt.toString(), td);
			}
			// td.setAtomValue(td.getAtomValue() + sd.getAtomValue());
			td.setAccumulateValue(td.getAccumulateValue()
					+ sd.getAccumulateValue());
		}
	}

	/**
	 * 从一个list里查找一个数据项
	 * 
	 * @param dataSets
	 *            数据项集合
	 * @param example
	 *            数据项特征样例
	 * @return null 如果没有找到
	 * */
	public <T extends Comparable<T>> T findByCode(Collection<T> dataSets,
			T example) {
		if (CollectionUtils.isEmpty(dataSets))
			return null;
		T tmp = null;
		List<T> ls = Lists.newArrayList();
		ls.addAll(dataSets);
		Collections.sort(ls);
		int idx = Collections.binarySearch(ls, example);
	
		if (idx >= 0) {// 找到
			tmp = ls.get(idx);
		} else {
			tmp = null;
		}
		return tmp;
	}
	/**
	 * 从一个list里查找一个数据项
	 * 
	 * @param dataSets
	 *            数据项集合
	 * @param example
	 *            数据项特征样例
	 * @return null 如果没有找到
	 * */
	public <T extends Comparable<T>> T find(Collection<T> dataSets,
			T example) {
		if (CollectionUtils.isEmpty(dataSets))
			return null;
		T tmp = null;
		List<T> ls = Lists.newArrayList();
		ls.addAll(dataSets);
		Collections.sort(ls);
		int idx = Collections.binarySearch(ls, example);
		if (idx >= 0) {// 找到
			tmp = ls.get(idx);
		} else {
			tmp = null;
		}
		return tmp;
	}
	
	public <T extends Comparable<T>> T find(Collection<T> dataSets,
			T example,Comparator<T> cond) {
		if (CollectionUtils.isEmpty(dataSets))
			return null;
		T tmp = null;
		List<T> ls = Lists.newArrayList();
		ls.addAll(dataSets);
		Collections.sort(ls);
		int idx = Collections.binarySearch(ls, example,cond);
		
		if (idx >= 0) {// 找到
			tmp = ls.get(idx);
		} else {
			tmp = null;
		}
		return tmp;
	}

	/**
	 * 根据key,映射计划
	 * 
	 * @param key
	 *            部门
	 * @param value
	 *            值
	 * @param mapper
	 *            计划map
	 * 
	 * 
	 * */
	public void mapperPlaning(String key, Collection<MonthPlanningItem> value,
			Map<String, Collection<MonthPlanningItem>> mapper) {
		Collection<MonthPlanningItem> tlist = mapper.get(key);
		if (tlist == null) {
			tlist = Sets.newHashSet();
			mapper.put(key, tlist);
		}
		tlist.addAll(value);
	}

}
